package com.adv.web.controller;

import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adv.web.common.BaseController;
import com.adv.web.entity.WxJsApiSignVO;

/**
 * 处理微信公共部分
 * <pre>微信公共</pre>
 * @author zhouxin
 * @date 2017年4月14日
 * @version v1.0
 */
@Controller
@RequestMapping(value = "/wx/config")
public class ConfigWeiXinController extends BaseController{
	
	@Autowired
	private WxMpService wxMpService;
	
	@Autowired
	private WxMpInMemoryConfigStorage wxMpConfigStorage;

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 
	 * @Title: getWxJsapiSignature
	 * @Description: TODO(获取微信config)
	 * @param url 前台访问url
	 * @throws WxErrorException 
	 * @author zhouxin
	 * @date 2016年3月8日 上午9:40:53
	 */
	@RequestMapping(value = "getWxJsapiSignature")
	@ResponseBody
	public WxJsApiSignVO getWxJsapiSignature(String url) throws WxErrorException {
		
		WxJsApiSignVO jsapiSign = new WxJsApiSignVO();
		WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(url);
		jsapiSign.setResultCode(0);
		jsapiSign.setAppId(wxMpConfigStorage.getAppId());
		jsapiSign.setNoncestr(jsapiSignature.getNoncestr());
		jsapiSign.setSignature(jsapiSignature.getSignature());
		jsapiSign.setTimestamp(jsapiSignature.getTimestamp());
		return jsapiSign;

	}
	
}
